<script setup lang='ts'>
import { ref, watch } from 'vue';
import { doToDoFenMiao, duToDoFen, duFenMiaoToDu, duFenToDo } from "./base/duTransform"

type Number2 = [number, number]

const props = withDefaults(defineProps<{
    modelValue?: Number2,
    defaultValue?: Number2,
    withUndefined?: boolean
    readonly?: boolean
    propertyName?: string,
    positionType?: string
}>(), { withUndefined: true, readonly: false, propertyName: '属性值', modelValue: () => [1, 1], positionType: '' })//positionType: 'duFenMiao' ||'duFen'

const emits = defineEmits(["update:modelValue"]);
const timers = ref<NodeJS.Timeout>()

const inputHandler = (e: any, numberValues: Number2 | undefined, num: number) => {
    clearTimeout(timers.value)
    timers.value = setTimeout(() => {
        if (numberValues === undefined) return
        let numberArr = [...numberValues]
        numberArr[num] = Number(e.target.value)
        emits('update:modelValue', numberArr)
    }, 200);
}

const longitude = ref()
const latitude = ref()
//度分秒
const duFenMiaoLongitud = ref<[number, number, number]>([0, 0, 0])//经度
const duFenMiaoLatitude = ref<[number, number, number]>([0, 0, 0])//纬度
//度分
const duFenLongitude = ref<[number, number]>([0, 0])//经度
const duFenLatitude = ref<[number, number]>([0, 0])//纬度
watch(() => props.modelValue, (val) => {
    if (props.positionType === 'duFenMiao') {
        duFenMiaoLongitud.value = doToDoFenMiao(val[0])
        duFenMiaoLatitude.value = doToDoFenMiao(val[1])
    } else if (props.positionType === 'duFen') {
        duFenLongitude.value = duToDoFen(val[0])
        duFenLatitude.value = duToDoFen(val[1])
    } else {
        longitude.value = val[0].toFixed(7)
        latitude.value = val[1].toFixed(7)
    }
}, { immediate: true })
const update = (e: any, numberValues: Number2 | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    numberArr[num] = Number(e.target.value)
    emits("update:modelValue", numberArr)
}
//度分秒
const update2 = (numberValues: Number2 | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    if (num === 0) {
        const du = duFenMiaoToDu(duFenMiaoLongitud.value)
        numberArr[num] = du
    } else if (num === 1) {
        const du = duFenMiaoToDu(duFenMiaoLatitude.value)
        numberArr[num] = du
    }
    emits("update:modelValue", numberArr)
}
//度分
const update3 = (numberValues: Number2 | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    if (num === 0) {
        const du = duFenToDo(duFenLongitude.value)
        numberArr[num] = du
    } else if (num === 1) {
        const du = duFenToDo(duFenLatitude.value)
        numberArr[num] = du
    }
    emits("update:modelValue", numberArr)
}
</script>

<template>
    <div style="width: 100%;" v-if="propertyName === '二维坐标'">
        <div class="position" v-if="positionType === 'duFenMiao'">
            <div class="input2" style="margin-bottom: 6px;">
                <label for=""> {{ '经度' }}</label>
                <div class="point_dufenmiao">
                    <input type="number" class="input_number" v-model="duFenMiaoLongitud[0]"
                        @blur="update2(modelValue, 0)" @keydown.enter="update2(modelValue, 0)"
                        placeholder="请输入" />
                        <span>{{ '°' }}</span>
                    <input type="number" class="input_number" v-model="duFenMiaoLongitud[1]"
                        @blur="update2(modelValue, 0)" @keydown.enter="update2(modelValue, 0)"
                        placeholder="请输入" />
                        <span>{{ "'" }}</span>
                    <input type="number" class="input_number" v-model="duFenMiaoLongitud[2]"
                        @blur="update2(modelValue, 0)" @keydown.enter="update2(modelValue, 0)" placeholder="请输入" />
                    <span>{{ '"' }}</span>
                </div>
            </div>
            <div class="input2" style="margin-bottom: 6px;">
                <label for=""> {{ '纬度' }}</label>
                <div class="point_dufenmiao">
                    <input type="number" class="input_number" v-model="duFenMiaoLatitude[0]"
                        @blur="update2(modelValue, 1)" @keydown.enter="update2(modelValue, 1)"
                        placeholder="请输入" /><span>{{ '°' }}</span>
                    <input type="number" class="input_number" v-model="duFenMiaoLatitude[1]"
                        @blur="update2(modelValue, 1)" @keydown.enter="update2(modelValue, 1)"
                        placeholder="请输入" /><span>{{ "'" }}</span>
                    <input type="number" class="input_number" v-model="duFenMiaoLatitude[2]"
                        @blur="update2(modelValue, 1)" @keydown.enter="update2(modelValue, 1)"
                        placeholder="请输入" /><span>{{ '"' }}</span>
                </div>
            </div>
        </div>
        <div class="position" v-else-if="positionType === 'duFen'">
            <div class="input2" style="margin-bottom: 6px;">
                <label for=""> {{ '经度' }}</label>
                <div class="point_dufen">
                    <input type="number" class="input_number" v-model="duFenLongitude[0]" @blur="update3(modelValue, 0)"
                        @keydown.enter="update3(modelValue, 0)" placeholder="请输入" /><span>{{ '°' }}</span>
                    <input type="number" class="input_number" v-model="duFenLongitude[1]" @blur="update3(modelValue, 0)"
                        @keydown.enter="update3(modelValue, 0)" placeholder="请输入" /><span>{{ "'" }}</span>
                </div>
            </div>
            <div class="input2" style="margin-bottom: 6px;">
                <label for=""> {{ '纬度' }}</label>
                <div class="point_dufen">
                    <input type="number" class="input_number" v-model="duFenLatitude[0]" @blur="update3(modelValue, 1)"
                        @keydown.enter="update3(modelValue, 1)" placeholder="请输入" /><span>{{ '°' }}</span>
                    <input type="number" class="input_number" v-model="duFenLatitude[1]" @blur="update3(modelValue, 1)"
                        @keydown.enter="update3(modelValue, 1)" placeholder="请输入" /><span>{{ "'" }}</span>
                </div>
            </div>
        </div>
        <div v-else>
            <div class="input2" style="margin-bottom:7px ;">
                <label for="">{{ '经度' }}</label>
                <input type="number" class="input_number" :value="longitude" @blur="update($event, modelValue, 0)"
                    @keydown.enter="update($event, modelValue, 0)" step="0.0000001" placeholder="请输入经度" />
            </div>
            <div class="input2">
                <label for="">{{ '纬度' }}</label>
                <input type="number" class="input_number" :value="latitude" @blur="update($event, modelValue, 1)"
                    @keydown.enter="update($event, modelValue, 1)" step="0.0000001" placeholder="请输入纬度" />
            </div>
        </div>
    </div>
    <div v-else>
        <div class="input2">
            <input v-for="(_, index) in modelValue" :key="index" type="number" class="input_number" :disabled="readonly"
                :value="modelValue[index]" @input="inputHandler($event, modelValue, index)" />
        </div>
    </div>
</template>

<style scoped>
.input2 {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.input2>label {
    width: 70px;
    color: #fff;
    font-size: 14px;
    color: rgba(230, 230, 230, 1);
    font-size: 14px;
}

.input2>input {
    flex: 1;
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px px;
    box-sizing: border-box;
}

.input2>input {
    display: inline-block;
    width: 90px;
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px;
    box-sizing: border-box;
}

.input2>input:hover,.input2>.point_dufenmiao>input:hover ,.input2>.point_dufen>input:hover{
    border: 1px solid rgba(44, 104, 247, 1);
}

.input2>input:focus,.input2>.point_dufenmiao>input:focus ,.input2>.point_dufen>input:focus {
    border: 1px solid #2c68f7;
}


input::selection {
    background: rgb(0, 200, 255);
    color: rgb(23, 22, 23);
}

.input_number::-webkit-outer-spin-button,
.input_number::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

.input_number[type="number"] {
    -moz-appearance: textfield;
}

.position {
    width: 100%;
    border-top: 1px solid #111111;
    border-bottom: 1px solid #111111;
    padding-top: 10px;
    padding-bottom: 10px;
}

.input2>.point_dufenmiao {
    display: flex;
    flex: 1;
    height: 26px;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
}

.input2>.point_dufenmiao>input {
    display: inline-block;
    width: 30.3%;
    /* flex: 1; */
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px;
    box-sizing: border-box;
}

.input2>.point_dufenmiao>span {
    display: inline-block;
    width: 3%;
}

.input2>.point_dufen {
    display: flex;
    flex: 1;
    height: 26px;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
}

.input2>.point_dufen>input {
    display: inline-block;
    width: 47%;
    /* flex: 1; */
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px;
    box-sizing: border-box;
}

.input2>.point_dufen>span {
    display: inline-block;
    width: 3%;
}
</style>
